{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "首先 import 必要的模块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import lightgbm as lgbm\n",
    "#from lgbm.sklearn import LGBMClassifier\n",
    "import xlearn as xl\n",
    "import pandas as pd \n",
    "import numpy as np\n",
    "import time\n",
    "\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "from sklearn.model_selection import StratifiedKFold\n",
    "from sklearn.metrics import log_loss     #采用logloss作为评价指标\n",
    "from pandas import DataFrame\n",
    "\n",
    "from sklearn.metrics import log_loss\n",
    "from sklearn.preprocessing import OneHotEncoder\n",
    "from matplotlib import pyplot\n",
    "import seaborn as sns\n",
    "%matplotlib inline\n",
    "from libffm_data_generate import *"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.1读取数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_name_1='800_0'\n",
    "train_name_2='800_1'\n",
    "valid_name_1='800_2'\n",
    "valid_name_2='800_3'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(3996246, 23)\n",
      "(3886316, 23)\n"
     ]
    }
   ],
   "source": [
    "#train_data\n",
    "train_1 = pd.read_csv(\"/home/jupyter/训练数据组/400w数据组/\"+train_name_1,index_col=0).reset_index(drop=True)   #拿200万条数据训练\n",
    "train_1.drop(['id','hour','user_id','Unnamed: 0'],axis=1,inplace=True)\n",
    "print(train_1.shape)\n",
    "#validation_data\n",
    "valid_1 = pd.read_csv(\"/home/jupyter/训练数据组/400w数据组/\"+valid_name_1,index_col=0).reset_index(drop=True)   #拿200万条数据训练\n",
    "valid_1.drop(['id','hour','user_id','Unnamed: 0'],axis=1,inplace=True)\n",
    "print(valid_1.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(999864, 23)\n",
      "(996224, 23)\n"
     ]
    }
   ],
   "source": [
    "#train_data\n",
    "train_2 = pd.read_csv(\"/home/jupyter/训练数据组/100w数据组/\"+train_name_2,index_col=0).reset_index(drop=True)   #拿200万条数据训练\n",
    "train_2.drop(['id','hour','user_id','Unnamed: 0'],axis=1,inplace=True)\n",
    "print(train_2.shape)\n",
    "valid_2 = pd.read_csv(\"/home/jupyter/训练数据组/100w数据组/\"+valid_name_2,index_col=0).reset_index(drop=True)   #拿200万条数据训练\n",
    "valid_2.drop(['id','hour','user_id','Unnamed: 0'],axis=1,inplace=True)\n",
    "print(valid_2.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/qiaoliang/anaconda3/lib/python3.6/site-packages/numpy/lib/arraysetops.py:472: FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison\n",
      "  mask |= (ar1 == a)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "(4218938, 23)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#test_data\n",
    "test = pd.read_csv(\"/home/jupyter/训练数据组/FE_test_26_final.csv\",index_col=0).reset_index(drop=True)   #拿200万条数据训练\n",
    "test.drop(['id','hour','user_id'],axis=1,inplace=True)\n",
    "test.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "#first\n",
    "y_train_1 = train_1['click']\n",
    "x_train_1 = train_1.drop([\"click\"], axis=1)\n",
    "\n",
    "y_valid_1 = valid_1['click']\n",
    "x_valid_1 = valid_1.drop([\"click\"], axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "#second\n",
    "y_train_2 = train_2['click']\n",
    "x_train_2 = train_2.drop([\"click\"], axis=1)\n",
    "\n",
    "y_valid_2 = valid_2['click']\n",
    "x_valid_2 = valid_2.drop([\"click\"], axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "y_test = test['click']\n",
    "x_test = test.drop([\"click\"], axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(4218938,)"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_test.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.2 original_data generate libffm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "157.61609506607056\n",
      "157.64250016212463\n"
     ]
    }
   ],
   "source": [
    "#ffm-data\n",
    "#train\n",
    "start=time.time()\n",
    "original_ffm_data(x=x_train_1,y=y_train_1,save_name=r'/home/jupyter/xlearn/ffm/ffm_'+train_name_1+'.txt')\n",
    "end=time.time()\n",
    "print(end-start)\n",
    "#valid\n",
    "start=time.time()\n",
    "original_ffm_data(x=x_valid_1,y=y_valid_1,save_name=r'/home/jupyter/xlearn/ffm/ffm_'+valid_name_1+'.txt')\n",
    "end=time.time()\n",
    "print(end-start)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "aa=pd.read_csv(r'/home/jupyter/xlearn/ffm/'+'ffm_test.txt')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(4218937, 1)"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "aa.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "174.09121370315552\n",
      "173.4769024848938\n",
      "754.1248562335968\n"
     ]
    }
   ],
   "source": [
    "#test\n",
    "start=time.time()\n",
    "original_ffm_data(x=x_test,y=y_test,save_name=r'/home/jupyter/xlearn/ffm/'+'ffm_test.txt')\n",
    "end=time.time()\n",
    "print(end-start)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "#fm_data\n",
    "x_train_fm_1=np.array(x_train_1)\n",
    "x_valid_fm_1=np.array(x_valid_1)\n",
    "x_test_fm_1=np.array(x_test)\n",
    "xl.write_data_to_xlearn_format(X=x_train_fm_1,y=y_train_1,filepath=r'/home/jupyter/xlearn/fm'+train_1_name+'.txt')\n",
    "xl.write_data_to_xlearn_format(X=x_valid_fm_1,y=y_valid_1,filepath=r'/home/jupyter/xlearn/fm'+valid_name_1+'.txt')\n",
    "xl.write_data_to_xlearn_format(X=x_test_fm_1,y=y_test,filepath=r'/home/jupyter/xlearn/fm'+'fm_test.txt')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "del train_1\n",
    "del train_2\n",
    "del valid_1\n",
    "del valid_2\n",
    "del test"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "默认参数，此时学习率为0.1，比较大，观察弱分类数目的大致范围\n",
    "（采用默认参数配置，看看模型是过拟合还是欠拟合）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2. lightgbm train"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/qiaoliang/anaconda3/lib/python3.6/site-packages/lightgbm/engine.py:102: UserWarning: Found `num_boost_round` in params. Will use it instead of argument\n",
      "  warnings.warn(\"Found `{}` in params. Will use it instead of argument\".format(alias))\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.389890193939209\n"
     ]
    }
   ],
   "source": [
    "start=time.time()\n",
    "# 定义模型\n",
    "lgb1 = lgbm.LGBMRegressor(\n",
    "          num_boost_round=50,\n",
    "          boosting='gbdt', \n",
    "          objective='binary', \n",
    "          num_threads=6, \n",
    "          #default=OpenMP_default, type=int, alias=num_thread, nthread LightGBM 的线程数\n",
    "#          'silent': True,\n",
    "          learning_rate=0.1, \n",
    "          num_leaves=24, \n",
    "          #num_leaves：叶子数\n",
    "#num_leaves, default=31, type=int, alias=num_leaf一棵树上的叶子数\n",
    "          max_depth= 5,\n",
    "          max_bi= 256, \n",
    "          subsample_for_bin= 200000,#默认200000，用来构建直方图数据数量\n",
    "          subsample= 0.8, #默认1\n",
    "          #类似于feature_fraction,它将在不进行重采样的情况下\n",
    "          #随机选择部分数据可以用来加速训练可以用来处理过拟合Not\n",
    "          subsample_freq= 1, \n",
    "          colsample_bytree= 0.8,       \n",
    "          reg_alpha= 0,         \n",
    "          reg_lambda= 0, \n",
    "          min_split_gain=0.0, \n",
    "          min_child_weight= 1,        \n",
    "          min_child_samples= 100, \n",
    "          min_data_in_leaf=50, \n",
    "          #default=20, type=int, alias=min_data_per_leaf , min_data, \n",
    "          #min_child_samples一个叶子上数据的最小数量. 可以用来处理过拟合.\n",
    "        \n",
    "        # 'categorical_feature':3\n",
    "         # 'categorical_feature':3:26\n",
    "         )\n",
    "# 训练学习\n",
    "model=lgb1.fit(x_train_1, y_train_1)\n",
    "end=time.time()\n",
    "print(end-start)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "logloss of train : 0.41508363815322297\n",
      "logloss of valid : 0.4154014815745415\n"
     ]
    },
    {
     "ename": "NameError",
     "evalue": "name 'x_test' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-8-e7798d5f2408>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m     10\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     11\u001b[0m \u001b[0;31m# test上logloss评测\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 12\u001b[0;31m \u001b[0my_pred_test\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mlgb1\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpredict\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx_test\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     13\u001b[0m \u001b[0mlogloss\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mlog_loss\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0my_test\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0my_pred_test\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     14\u001b[0m \u001b[0mprint\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;34m\"logloss of test :\"\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mlogloss\u001b[0m \u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mNameError\u001b[0m: name 'x_test' is not defined"
     ]
    }
   ],
   "source": [
    "# train上logloss评测\n",
    "y_pred_train = lgb1.predict(x_train_1)\n",
    "logloss = log_loss(y_train_1, y_pred_train)\n",
    "print (\"logloss of train :\",logloss )\n",
    "\n",
    "# valid上logloss评测\n",
    "y_pred_valid = lgb1.predict(x_valid_1)\n",
    "logloss = log_loss(y_valid_1, y_pred_valid)\n",
    "print (\"logloss of valid :\",logloss )\n",
    "\n",
    "# test上logloss评测\n",
    "y_pred_test = lgb1.predict(x_test)\n",
    "logloss = log_loss(y_test, y_pred_test)\n",
    "print (\"logloss of test :\",logloss )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(999864, 50)\n",
      "(999864, 50)\n",
      "(4218938, 50)\n"
     ]
    }
   ],
   "source": [
    "x_train_leaves = lgb1.predict(x_train_1, pred_leaf=True)    \n",
    "x_valid_leaves = lgb1.predict(x_valid_1, pred_leaf=True) \n",
    "x_test_leaves = lgb1.predict(x_test, pred_leaf=True) \n",
    "print(x_train_leaves.shape)\n",
    "print(x_valid_leaves.shape)\n",
    "print(x_test_leaves.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(999864, 50)\n",
      "(996224, 50)\n"
     ]
    }
   ],
   "source": [
    "x_train_leaves = lgb1.predict(x_train_2, pred_leaf=True)    \n",
    "x_valid_leaves = lgb1.predict(x_valid_2, pred_leaf=True) \n",
    "print(x_train_leaves.shape)\n",
    "print(x_valid_leaves.shape)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1、生成libffm所需的格式"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### $$$ 执行代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "427.5567891597748\n"
     ]
    }
   ],
   "source": [
    "start=time.time()\n",
    "ffm_data_t(x_train_leaves=x_train_leaves,y_train=y_train,save_name=r'/usr/local/ffm_train.txt')\n",
    "end=time.time()\n",
    "print(end-start)\n",
    "\n",
    "start=time.time()\n",
    "ffm_data_t(x_train_leaves=x_test_leaves,y_train=y_test,save_name=r'/usr/local/ffm_test.txt')\n",
    "end=time.time()\n",
    "print(end-start)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "scrolled": true
   },
   "source": [
    "## x-learn train"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1. from leaf to libffm data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(999864,)"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_train_1.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "#用于gbm训练的数据，直接进入ffm\n",
    "# self def to produce libffm data\n",
    "start=time.time()\n",
    "xl.write_data_to_xlearn_format(X=x_train_leaves,y=y_train_2,filepath=r'/home/jupyter/xlearn/fm_train.txt')\n",
    "xl.write_data_to_xlearn_format(X=x_valid_leaves,y=y_valid_2,filepath=r'/home/jupyter/xlearn/fm_valid.txt')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "xl.write_data_to_xlearn_format(X=x_test_leaves,y=y_test,filepath=r'/home/jupyter/xlearn/fm_test.txt')\n",
    "end=time.time()\n",
    "print(end-start)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.ffm模型训练-100w数据集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 原始数据进入ffm初始化\n",
    "ffm_model = xl.create_ffm() \n",
    "ffm_model.setTrain(r'/home/jupyter/xlearn/ffm/ffm_'+train_name_1+'.txt')  # 设置训练数据\n",
    "ffm_model.setValidate(r'/home/jupyter/xlearn/ffm/ffm_'+valid_name_1+'.txt')  #设置验证数据\n",
    "ffm_model.setTest(r'/home/jupyter/xlearn/ffm/ffm_test.txt')  # test data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "# lightgbm 进入ffm初始化\n",
    "#ffm_model = xl.create_ffm() # Use field-aware factorization machine\n",
    "#ffm_model.setTrain(r'/home/jupyter/xlearn/fm_train.txt')  # 设置训练数据\n",
    "#ffm_model.setValidate(r'/home/jupyter/xlearn/fm_valid.txt')  #设置验证数据\n",
    "#ffm_model.setTest(r'/home/jupyter/xlearn/fm_test.txt') "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1236.9563615322113\n"
     ]
    }
   ],
   "source": [
    "# param:\n",
    "#  0. binary classification\n",
    "#  1. learning rate: 0.2\n",
    "#  2. regular lambda: 0.002\n",
    "#  3. evaluation metric: accuracystart=time.time()\n",
    "param = {'task':'binary','k':4,'opt':'adagrad','lr':0.2,'lambda':0.0001, \n",
    "         'metric':'acc','fold':2,'epoch':26,'stop_window':3}\n",
    "\n",
    "#ffm_model.disableNorm()    # disable Instance-wise Normalization\n",
    "\n",
    "ffm_model.setSign()  # Convert output to 0-1  scope\n",
    "result=ffm_model.fit(param, r'/home/jupyter/xlearn/ffm/model.out')\n",
    "result_cv=ffm_model.cv(param)\n",
    "# Prediction task\n",
    "end=time.time()\n",
    "print(end-start)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "time.sleep(5)\n",
    "ffm_model.predict(r'/home/jupyter/xlearn/ffm/model.out', r'/home/jupyter/xlearn/output.txt')\n",
    "#0.40547"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3、opt选择连接ftrl调优-100w数据集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1780.3661370277405\n"
     ]
    }
   ],
   "source": [
    "param = {'task':'binary','k':4,'opt':'ftrl','lr':0.2,'lambda':0.0001, \n",
    "         'metric':'acc','fold':2,'epoch':26,'stop_window':3,\n",
    "        'alpha':0.002,'beta':0.8,'lambda_1':0.001,'lambda_2':1}\n",
    "\n",
    "ffm_model.setSign()  # Convert output to 0-1  scope\n",
    "result=ffm_model.fit(param, r'/home/jupyter/xlearn/ffm/model.out')\n",
    "result_cv=ffm_model.cv(param)\n",
    "# Prediction task\n",
    "end=time.time()\n",
    "print(end-start)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2026.9213027954102\n"
     ]
    }
   ],
   "source": [
    "param = {'task':'binary','k':4,'opt':'ftrl','lr':0.2,'lambda':0.0001, \n",
    "         'metric':'acc','fold':2,'epoch':26,'stop_window':3,\n",
    "        'alpha':0.002,'beta':0.8,'lambda_1':0.001,'lambda_2':0.1}\n",
    "\n",
    "ffm_model.setSign()  # Convert output to 0-1  scope\n",
    "result=ffm_model.fit(param, r'/home/jupyter/xlearn/ffm/model.out')\n",
    "result_cv=ffm_model.cv(param)\n",
    "# Prediction task\n",
    "end=time.time()\n",
    "print(end-start)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4、ffm训练在400w数据集上"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 4.1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 原始数据进入ffm初始化\n",
    "ffm_model = xl.create_ffm() \n",
    "ffm_model.setTrain(r'/home/jupyter/xlearn/ffm/ffm_'+train_name_1+'.txt')  # 设置训练数据\n",
    "ffm_model.setValidate(r'/home/jupyter/xlearn/ffm/ffm_'+valid_name_1+'.txt')  #设置验证数据\n",
    "ffm_model.setTest(r'/home/jupyter/xlearn/ffm/ffm_test.txt')  # test data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "285.26229786872864\n"
     ]
    }
   ],
   "source": [
    "start=time.time()\n",
    "# param:\n",
    "#  0. binary classification\n",
    "#  1. learning rate: 0.2\n",
    "#  2. regular lambda: 0.002\n",
    "#  3. evaluation metric: accuracystart=time.time()\n",
    "param = {'task':'binary','k':4,'opt':'adagrad','lr':0.2,'lambda':0.0001, \n",
    "         'metric':'acc','fold':2,'epoch':26,'stop_window':3}\n",
    "\n",
    "#ffm_model.disableNorm()    # disable Instance-wise Normalization\n",
    "ffm_model.setSigmoid()\n",
    "#ffm_model.setSign()  # Convert output to 0-1  scope\n",
    "result=ffm_model.fit(param, r'/home/jupyter/xlearn/ffm/model_400.out')\n",
    "result_cv=ffm_model.cv(param)\n",
    "# Prediction task\n",
    "end=time.time()\n",
    "print(end-start)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "logloss of test : 0.40499725689989713\n"
     ]
    }
   ],
   "source": [
    "time.sleep(5)\n",
    "ffm_model.setSigmoid()\n",
    "ffm_model.predict(r'/home/jupyter/xlearn/ffm/model_400.out', r'/home/jupyter/xlearn/ffm/output_400.txt')\n",
    "#0.40547\n",
    "y_ffm_predict=pd.read_csv(r'/home/jupyter/xlearn/ffm/output_400.txt',header=None)\n",
    "logloss = log_loss(y_test, y_ffm_predict)\n",
    "print (\"logloss of test :\",logloss )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 4_2_1 lambda=0.00001"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "260.5236761569977\n"
     ]
    }
   ],
   "source": [
    "start=time.time()\n",
    "# param:\n",
    "#  0. binary classification\n",
    "#  1. learning rate: 0.2\n",
    "#  2. regular lambda: 0.002\n",
    "#  3. evaluation metric: accuracystart=time.time()\n",
    "param = {'task':'binary','k':4,'opt':'adagrad','lr':0.2,'lambda':0.00001, \n",
    "         'metric':'acc','fold':2,'epoch':26,'stop_window':3}\n",
    "\n",
    "#ffm_model.disableNorm()    # disable Instance-wise Normalization\n",
    "ffm_model.setSigmoid()\n",
    "#ffm_model.setSign()  # Convert output to 0-1  scope\n",
    "result=ffm_model.fit(param, r'/home/jupyter/xlearn/ffm/model_400.out')\n",
    "result_cv=ffm_model.cv(param)\n",
    "# Prediction task\n",
    "end=time.time()\n",
    "print(end-start)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "logloss of test : 0.40565516652332156\n"
     ]
    }
   ],
   "source": [
    "time.sleep(5)\n",
    "ffm_model.setSigmoid()\n",
    "ffm_model.predict(r'/home/jupyter/xlearn/ffm/model_400.out', r'/home/jupyter/xlearn/ffm/output_400.txt')\n",
    "#0.40547\n",
    "y_ffm_predict=pd.read_csv(r'/home/jupyter/xlearn/ffm/output_400.txt',header=None)\n",
    "logloss = log_loss(y_test, y_ffm_predict)\n",
    "print (\"logloss of test :\",logloss )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### lambda=0.000025"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "296.2349212169647\n",
      "logloss of test : 0.40428048256343824\n"
     ]
    }
   ],
   "source": [
    "start=time.time()\n",
    "# param:\n",
    "#  0. binary classification\n",
    "#  1. learning rate: 0.2\n",
    "#  2. regular lambda: 0.002\n",
    "#  3. evaluation metric: accuracystart=time.time()\n",
    "param = {'task':'binary','k':4,'opt':'adagrad','lr':0.2,'lambda':0.000025, \n",
    "         'metric':'acc','fold':2,'epoch':26,'stop_window':3}\n",
    "\n",
    "#ffm_model.disableNorm()    # disable Instance-wise Normalization\n",
    "ffm_model.setSigmoid()\n",
    "#ffm_model.setSign()  # Convert output to 0-1  scope\n",
    "result=ffm_model.fit(param, r'/home/jupyter/xlearn/ffm/model_400.out')\n",
    "result_cv=ffm_model.cv(param)\n",
    "# Prediction task\n",
    "end=time.time()\n",
    "print(end-start)\n",
    "\n",
    "time.sleep(15)\n",
    "ffm_model.setSigmoid()\n",
    "ffm_model.predict(r'/home/jupyter/xlearn/ffm/model_400.out', r'/home/jupyter/xlearn/ffm/output_400.txt')\n",
    "#0.40547\n",
    "y_ffm_predict=pd.read_csv(r'/home/jupyter/xlearn/ffm/output_400.txt',header=None)\n",
    "logloss = log_loss(y_test, y_ffm_predict)\n",
    "print (\"logloss of test :\",logloss )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### lambda=0.00005"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "319.87837266921997\n"
     ]
    }
   ],
   "source": [
    "start=time.time()\n",
    "# param:\n",
    "#  0. binary classification\n",
    "#  1. learning rate: 0.2\n",
    "#  2. regular lambda: 0.002\n",
    "#  3. evaluation metric: accuracystart=time.time()\n",
    "param = {'task':'binary','k':4,'opt':'adagrad','lr':0.2,'lambda':0.00005, \n",
    "         'metric':'acc','fold':2,'epoch':26,'stop_window':3}\n",
    "\n",
    "#ffm_model.disableNorm()    # disable Instance-wise Normalization\n",
    "ffm_model.setSigmoid()\n",
    "#ffm_model.setSign()  # Convert output to 0-1  scope\n",
    "result=ffm_model.fit(param, r'/home/jupyter/xlearn/ffm/model_400.out')\n",
    "result_cv=ffm_model.cv(param)\n",
    "# Prediction task\n",
    "end=time.time()\n",
    "print(end-start)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "logloss of test : 0.40474737996021176\n"
     ]
    }
   ],
   "source": [
    "time.sleep(15)\n",
    "#ffm_model.setSigmoid()\n",
    "ffm_model.predict(r'/home/jupyter/xlearn/ffm/model_400.out', r'/home/jupyter/xlearn/ffm/output_400.txt')\n",
    "#0.40547\n",
    "y_ffm_predict=pd.read_csv(r'/home/jupyter/xlearn/ffm/output_400.txt',header=None)\n",
    "logloss = log_loss(y_test, y_ffm_predict)\n",
    "print (\"logloss of test :\",logloss )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### lambda=0.0002"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "285.56197237968445\n",
      "logloss of test : 0.4051638143060699\n"
     ]
    }
   ],
   "source": [
    "start=time.time()\n",
    "# param:\n",
    "#  0. binary classification\n",
    "#  1. learning rate: 0.2\n",
    "#  2. regular lambda: 0.002\n",
    "#  3. evaluation metric: accuracystart=time.time()\n",
    "param = {'task':'binary','k':4,'opt':'adagrad','lr':0.2,'lambda':0.0002, \n",
    "         'metric':'acc','fold':2,'epoch':26,'stop_window':3}\n",
    "\n",
    "#ffm_model.disableNorm()    # disable Instance-wise Normalization\n",
    "ffm_model.setSigmoid()\n",
    "#ffm_model.setSign()  # Convert output to 0-1  scope\n",
    "result=ffm_model.fit(param, r'/home/jupyter/xlearn/ffm/model_400.out')\n",
    "result_cv=ffm_model.cv(param)\n",
    "# Prediction task\n",
    "end=time.time()\n",
    "print(end-start)\n",
    "\n",
    "time.sleep(15)\n",
    "#ffm_model.setSigmoid()\n",
    "ffm_model.predict(r'/home/jupyter/xlearn/ffm/model_400.out', r'/home/jupyter/xlearn/ffm/output_400.txt')\n",
    "#0.40547\n",
    "y_ffm_predict=pd.read_csv(r'/home/jupyter/xlearn/ffm/output_400.txt',header=None)\n",
    "logloss = log_loss(y_test, y_ffm_predict)\n",
    "print (\"logloss of test :\",logloss )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### epoch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "238.81746816635132\n"
     ]
    }
   ],
   "source": [
    "start=time.time()\n",
    "# param:\n",
    "#  0. binary classification\n",
    "#  1. learning rate: 0.2\n",
    "#  2. regular lambda: 0.002\n",
    "#  3. evaluation metric: accuracystart=time.time()\n",
    "param = {'task':'binary','k':4,'opt':'adagrad','lr':0.2,'lambda':0.0001, \n",
    "         'metric':'acc','fold':2,'epoch':20,'stop_window':3}\n",
    "\n",
    "#ffm_model.disableNorm()    # disable Instance-wise Normalization\n",
    "ffm_model.setSigmoid()\n",
    "#ffm_model.setSign()  # Convert output to 0-1  scope\n",
    "result=ffm_model.fit(param, r'/home/jupyter/xlearn/ffm/model_400.out')\n",
    "result_cv=ffm_model.cv(param)\n",
    "# Prediction task\n",
    "end=time.time()\n",
    "print(end-start)\n",
    "\n",
    "time.sleep(15)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "logloss of test : 0.4047878934880913\n"
     ]
    }
   ],
   "source": [
    "#ffm_model.setSigmoid()\n",
    "ffm_model.predict(r'/home/jupyter/xlearn/ffm/model_400.out', r'/home/jupyter/xlearn/ffm/output_400.txt')\n",
    "#0.40547\n",
    "y_ffm_predict=pd.read_csv(r'/home/jupyter/xlearn/ffm/output_400.txt',header=None)\n",
    "logloss = log_loss(y_test, y_ffm_predict)\n",
    "print (\"logloss of test :\",logloss )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### ffm在800万数据集上"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 原始数据进入ffm初始化\n",
    "ffm_model = xl.create_ffm() \n",
    "ffm_model.setTrain(r'/home/jupyter/xlearn/ffm/ffm_'+train_name_1+'.txt')  # 设置训练数据\n",
    "ffm_model.setValidate(r'/home/jupyter/xlearn/ffm/ffm_'+valid_name_1+'.txt')  #设置验证数据\n",
    "ffm_model.setTest(r'/home/jupyter/xlearn/ffm/ffm_test.txt')  # test data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "670.4300518035889\n"
     ]
    }
   ],
   "source": [
    "start=time.time()\n",
    "# param:\n",
    "#  0. binary classification\n",
    "#  1. learning rate: 0.2\n",
    "#  2. regular lambda: 0.002\n",
    "#  3. evaluation metric: accuracystart=time.time()\n",
    "param = {'task':'binary','k':4,'opt':'adagrad','lr':0.2,'lambda':0.0001, \n",
    "         'metric':'acc','fold':2,'epoch':26,'stop_window':3}\n",
    "\n",
    "#ffm_model.disableNorm()    # disable Instance-wise Normalization\n",
    "ffm_model.setSigmoid()\n",
    "#ffm_model.setSign()  # Convert output to 0-1  scope\n",
    "result=ffm_model.fit(param, r'/home/jupyter/xlearn/ffm/model_400.out')\n",
    "result_cv=ffm_model.cv(param)\n",
    "# Prediction task\n",
    "end=time.time()\n",
    "print(end-start)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "logloss of test : 0.4060380666747183\n"
     ]
    }
   ],
   "source": [
    "time.sleep(5)\n",
    "#ffm_model.setSigmoid()\n",
    "ffm_model.predict(r'/home/jupyter/xlearn/ffm/model_400.out', r'/home/jupyter/xlearn/ffm/output_400.txt')\n",
    "#0.40547\n",
    "y_ffm_predict=pd.read_csv(r'/home/jupyter/xlearn/ffm/output_400.txt',header=None)\n",
    "logloss = log_loss(y_test, y_ffm_predict)\n",
    "print (\"logloss of test :\",logloss )\n",
    "#"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 'lambda':0.00005"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "579.3012561798096\n"
     ]
    }
   ],
   "source": [
    "start=time.time()\n",
    "# param:\n",
    "#  0. binary classification\n",
    "#  1. learning rate: 0.2\n",
    "#  2. regular lambda: 0.002\n",
    "#  3. evaluation metric: accuracystart=time.time()\n",
    "param = {'task':'binary','k':4,'opt':'adagrad','lr':0.2,'lambda':0.00005, \n",
    "         'metric':'acc','fold':2,'epoch':26,'stop_window':3}\n",
    "\n",
    "#ffm_model.disableNorm()    # disable Instance-wise Normalization\n",
    "ffm_model.setSigmoid()\n",
    "#ffm_model.setSign()  # Convert output to 0-1  scope\n",
    "result=ffm_model.fit(param, r'/home/jupyter/xlearn/ffm/model_400.out')\n",
    "result_cv=ffm_model.cv(param)\n",
    "# Prediction task\n",
    "end=time.time()\n",
    "print(end-start)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "logloss of test : 0.4048570572361045\n"
     ]
    }
   ],
   "source": [
    "time.sleep(5)\n",
    "#ffm_model.setSigmoid()\n",
    "ffm_model.predict(r'/home/jupyter/xlearn/ffm/model_400.out', r'/home/jupyter/xlearn/ffm/output_400.txt')\n",
    "#0.40547\n",
    "y_ffm_predict=pd.read_csv(r'/home/jupyter/xlearn/ffm/output_400.txt',header=None)\n",
    "logloss = log_loss(y_test, y_ffm_predict)\n",
    "print (\"logloss of test :\",logloss )\n",
    "#"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 'lambda':0.00001"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "573.906849861145\n"
     ]
    }
   ],
   "source": [
    "start=time.time()\n",
    "# param:\n",
    "#  0. binary classification\n",
    "#  1. learning rate: 0.2\n",
    "#  2. regular lambda: 0.002\n",
    "#  3. evaluation metric: accuracystart=time.time()\n",
    "param = {'task':'binary','k':4,'opt':'adagrad','lr':0.2,'lambda':0.00001, \n",
    "         'metric':'acc','fold':2,'epoch':26,'stop_window':3}\n",
    "\n",
    "#ffm_model.disableNorm()    # disable Instance-wise Normalization\n",
    "ffm_model.setSigmoid()\n",
    "#ffm_model.setSign()  # Convert output to 0-1  scope\n",
    "result=ffm_model.fit(param, r'/home/jupyter/xlearn/ffm/model_400.out')\n",
    "result_cv=ffm_model.cv(param)\n",
    "# Prediction task\n",
    "end=time.time()\n",
    "print(end-start)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "logloss of test : 0.4089581619953243\n"
     ]
    }
   ],
   "source": [
    "time.sleep(5)\n",
    "#ffm_model.setSigmoid()\n",
    "ffm_model.predict(r'/home/jupyter/xlearn/ffm/model_400.out', r'/home/jupyter/xlearn/ffm/output_400.txt')\n",
    "#0.40547\n",
    "y_ffm_predict=pd.read_csv(r'/home/jupyter/xlearn/ffm/output_400.txt',header=None)\n",
    "logloss = log_loss(y_test, y_ffm_predict)\n",
    "print (\"logloss of test :\",logloss )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 'lambda':0.000075"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "561.7510893344879\n"
     ]
    }
   ],
   "source": [
    "start=time.time()\n",
    "# param:\n",
    "#  0. binary classification\n",
    "#  1. learning rate: 0.2\n",
    "#  2. regular lambda: 0.002\n",
    "#  3. evaluation metric: accuracystart=time.time()\n",
    "param = {'task':'binary','k':4,'opt':'adagrad','lr':0.2,'lambda':0.000075, \n",
    "         'metric':'acc','fold':2,'epoch':26,'stop_window':3}\n",
    "\n",
    "#ffm_model.disableNorm()    # disable Instance-wise Normalization\n",
    "ffm_model.setSigmoid()\n",
    "#ffm_model.setSign()  # Convert output to 0-1  scope\n",
    "result=ffm_model.fit(param, r'/home/jupyter/xlearn/ffm/model_400.out')\n",
    "result_cv=ffm_model.cv(param)\n",
    "# Prediction task\n",
    "end=time.time()\n",
    "print(end-start)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "logloss of test : 0.4060936461077703\n"
     ]
    }
   ],
   "source": [
    "time.sleep(5)\n",
    "#ffm_model.setSigmoid()\n",
    "ffm_model.predict(r'/home/jupyter/xlearn/ffm/model_400.out', r'/home/jupyter/xlearn/ffm/output_400.txt')\n",
    "#0.40547\n",
    "y_ffm_predict=pd.read_csv(r'/home/jupyter/xlearn/ffm/output_400.txt',header=None)\n",
    "logloss = log_loss(y_test, y_ffm_predict)\n",
    "print (\"logloss of test :\",logloss )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### epoch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1241.8230800628662\n"
     ]
    }
   ],
   "source": [
    "start=time.time()\n",
    "# param:\n",
    "#  0. binary classification\n",
    "#  1. learning rate: 0.2\n",
    "#  2. regular lambda: 0.002\n",
    "#  3. evaluation metric: accuracystart=time.time()\n",
    "param = {'task':'binary','k':4,'opt':'adagrad','lr':0.2,'lambda':0.000075, \n",
    "         'metric':'acc','fold':2,'epoch':50,'stop_window':3}\n",
    "\n",
    "#ffm_model.disableNorm()    # disable Instance-wise Normalization\n",
    "ffm_model.setSigmoid()\n",
    "#ffm_model.setSign()  # Convert output to 0-1  scope\n",
    "result=ffm_model.fit(param, r'/home/jupyter/xlearn/ffm/model_400.out')\n",
    "result_cv=ffm_model.cv(param)\n",
    "# Prediction task\n",
    "end=time.time()\n",
    "print(end-start)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "time.sleep(5)\n",
    "#ffm_model.setSigmoid()\n",
    "ffm_model.predict(r'/home/jupyter/xlearn/ffm/model_400.out', r'/home/jupyter/xlearn/ffm/output_400.txt')\n",
    "#0.40547\n",
    "y_ffm_predict=pd.read_csv(r'/home/jupyter/xlearn/ffm/output_400.txt',header=None)\n",
    "logloss = log_loss(y_test, y_ffm_predict)\n",
    "print (\"logloss of test :\",logloss )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "for x in range(1,5,2):\n",
    "    print(x)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
